﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace BallMove
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        bool _isMove = false;//是否移动
        //鼠标按下的位置
        Point _mouseDownPosition;
        //鼠标按下控件的位置
        Point _mouseDownControlPosition;
        Button _button = null;
        public MainWindow()
        {
            InitializeComponent();
            //RotateTransform rotateTransform = new RotateTransform();
            //qiu.RenderTransformOrigin = new Point(15, 0.5);
        }

        /// <summary>
        /// 鼠标按下时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void qiu_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            _isMove = true;
            _button = sender as Button;
            //相对于指定元素的鼠标位置（获取按下的点）
            _mouseDownPosition = e.GetPosition(this);
            TranslateTransform transform = _button.RenderTransform as TranslateTransform;
            if (transform == null)
            {
                transform = new TranslateTransform();
                _button.RenderTransform = transform;
            }
            _mouseDownControlPosition = new Point(transform.X, transform.Y);
            _button.CaptureMouse();
        }

        /// <summary>
        /// 鼠标移动时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void qiu_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (_isMove)
            {
                //相对于指定元素的鼠标位置（获取移动的点）
                var mouseMovePosition = e.GetPosition(myCanvas);

                //移动时鼠标的位置-鼠标按下时的位置
                var differencePosition = mouseMovePosition - _mouseDownPosition;

                #region 第一种拖动
                //{
                //    _button.SetValue(Canvas.LeftProperty, differencePosition.X);
                //    _button.SetValue(Canvas.TopProperty, differencePosition.Y);
                //}
                #endregion
                #region 第二种拖动
                {
                    //元素呈现位置的转换信息
                    TranslateTransform transform = _button.RenderTransform as TranslateTransform;
                    //按下的点 + 差异的点
                    transform.X = _mouseDownControlPosition.X + differencePosition.X;
                    transform.Y = _mouseDownControlPosition.Y + differencePosition.Y;
                }
                #endregion
            }
        }

        /// <summary>
        /// 鼠标弹起时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void qiu_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            _isMove = false;
            _button.ReleaseMouseCapture();
            _button = null;
        }
    }
}
